package main

import (
	"context"
	"fmt"
	grpc2 "go-guide/rpc/grpc"
	"google.golang.org/grpc"
	"io"
	"time"
)

func main() {
	conn, err := grpc.Dial(":6001", grpc.WithInsecure())
	if err != nil {
		fmt.Println(err.Error())
		return
	}

	defer conn.Close()
	client := grpc2.NewHelloServiceClient(conn)
	response, err := client.SayHello(context.Background(), &grpc2.HelloRequest{
		Username: "ark",
	})
	if err != nil {
		fmt.Println(err.Error())
		return
	}
	fmt.Println(response.Message)

	chatClilent, e := client.Chat(context.Background())
	if e != nil {
		fmt.Println(e.Error())
		return
	}
	go func() {
		for {
			stream, e := chatClilent.Recv()
			if e == io.EOF {
				fmt.Println("EOF")
				return
			}
			if e != nil {
				fmt.Println(e.Error())
				return
			}
			fmt.Println("receive from provider:", stream.Stream)
		}
	}()
	chatClilent.Send(&grpc2.ClientStream{
		Stream: newBytes(10, 9, 8, 7),
	})
	select {
	case <-time.After(20 * time.Second):
	}

}

func newBytes(a ...byte) []byte {
	return a
}
